package com.ruoyi.flowable.controller;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.flowable.common.engine.impl.Page;
import org.flowable.common.engine.impl.interceptor.Command;
import org.flowable.common.engine.impl.interceptor.CommandContext;
import org.flowable.engine.ManagementService;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.ui.modeler.domain.AbstractModel;
import org.flowable.ui.modeler.domain.Model;
import org.flowable.ui.modeler.serviceapi.ModelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.PageDomain;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.page.TableSupport;
import com.ruoyi.flowable.service.IModelExtService;

@Controller
@RequestMapping("/flowable/deployment")
public class DeploymentController extends BaseController {

    @Autowired
    protected ModelService modelService;

    @Autowired
    protected IModelExtService modelExtService;

    @Autowired
    protected ManagementService managementService;

    private String prefix = "flowable/deployment";

    @RequiresPermissions("flowable:model:view")
    @GetMapping()
    public String model()
    {
        return prefix + "/model";
    }

    /**
     * 查询流程模型列表
     */
    @RequiresPermissions("flowable:model:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Model model)
    {
        startPage();
        List<Model> modelList = managementService.executeCommand(new Command<List<Model>>() {
            @SuppressWarnings("unchecked")
            @Override
            public List<Model> execute(CommandContext commandContext) {
                return (List<Model>) CommandContextUtil.getDbSqlSession(commandContext).selectList("selectModels",model);
            }
        });
        return getDataTable(modelList);
    }

}
